package com.rmb.service.impl;

import com.rmb.client.EngineServiceClient;
import com.rmb.entity.EtlDataEngine;
import com.rmb.mapper.TaskConditionMapper;
import com.rmb.service.TaskConditionService;
import com.rmb.thread.ThreadManager;
import com.rmb.util.JsonToObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author 段段
 * @PackageName: com.rmb.service.impl
 * @ClassName: TaskConditionServiceImpl
 * @Description:
 * @data: 2021/1/10 19:14
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class TaskConditionServiceImpl implements TaskConditionService {

    @Autowired
    private TaskConditionMapper conditionMapper;

    @Autowired
    private EngineServiceClient engineServiceClient;

    private static final Integer ZORE=0;
    private static final String FAIL_NULL="";

    /**
     * 查询
     * @return
     */
    @Override
    public List<Map<String, Object>> conditionQuery(String jsonString) {
        /*判断json字符串是否有数据*/
        if (jsonString.length() <= ZORE || jsonString.equals(FAIL_NULL) || jsonString == null){
            return null;
        }
        /*拆解JSon字符串*/
        List<Map<String, Object>> list = JsonToObject.getJson(jsonString);
        /*拼接SQL*/
        String sql = JsonToObject.select(list);
        if (sql.length() <= ZORE|| sql == null){
            return null;
        }
        List<Map<String, Object>> maps = conditionMapper.conditionQuerySql(sql);
        /*查询楚若没有数据,返回null*/
        if (maps.size() <= ZORE){
            return null;
        }
        /*调用线程池,多线程添加到数据库*/
        multiThreading(maps);
        return maps;
    }

    @Override
    public List<Map<String, Object>> conditionQuery(String jsonString, Long id) {

        EtlDataEngine engineId = engineServiceClient.getEngineId(id);

        /*判断json字符串是否有数据*/
        if (jsonString.length() <= ZORE || jsonString.equals(FAIL_NULL) || jsonString == null){
            return null;
        }
        /*拆解JSon字符串*/
        List<Map<String, Object>> list = JsonToObject.getJson(jsonString);
        /*拼接SQL*/
        String sql = JsonToObject.select(list);
        if (sql.length() <= ZORE|| sql == null){
            return null;
        }
        List<Map<String, Object>> maps = conditionMapper.conditionQuerySql(sql);
        /*查询楚若没有数据,返回null*/
        if (maps.size() <= ZORE){
            return null;
        }
        /*调用线程池,多线程添加到数据库*/
        multiThreading(maps);
        return maps;
    }

    @Override
    public List<Map<String, Object>> conditionQuery(String jsonString, Long id, String jsonJoin) {
        EtlDataEngine engineId = engineServiceClient.getEngineId(id);

        /*判断json字符串是否有数据*/
        if (jsonString.length() <= ZORE || jsonString.equals(FAIL_NULL) || jsonString == null){
            return null;
        }
        /*拆解JSon字符串*/
        List<Map<String, Object>> list = JsonToObject.getJson(jsonString);
        /*拼接SQL*/
        String sql = JsonToObject.select(list,jsonJoin);
        if (sql.length() <= ZORE|| sql == null){
            return null;
        }
        List<Map<String, Object>> maps = conditionMapper.conditionQuerySql(sql);
        /*查询楚若没有数据,返回null*/
        if (maps.size() <= ZORE){
            return null;
        }
        /*调用线程池,多线程添加到数据库*/
        multiThreading(maps);
        return maps;
    }

    /**
     * 调用线程池,多线程添加到数据库
     */
    private void multiThreading(List<Map<String ,Object >> maps){
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    String insert = JsonToObject.insert(maps);
                    conditionMapper.insert(insert);
                    System.out.println("多线程成功");
                    Thread.sleep(5000);
                }catch (Exception e){
                    e.printStackTrace();
                    System.out.println("多线程异常失败");
                }
            }
        };
        ThreadManager.getThreadPoolProxy().execute(runnable);
    }

}
